package menu.lt.pers.menu;

import android.app.ActionBar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import menu.lt.pers.menu.adapter.SearchRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class Search_Activity extends FragmentActivity {

    private EditText et_search;
    private Button btn_search;

    private RecyclerView recyclerView;
    private LinearLayoutManager manager;
    private SearchRecyclerViewAdapter adapter;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private List<String> strings;
    private Gson gson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_);
        initView();
        initData();
        setListener();
        updataView();
    }



    private void initView() {
        android.app.ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            View layout = getLayoutInflater().inflate(R.layout.actionbar_search,
                    new LinearLayout(Search_Activity.this), false);
            et_search = layout.findViewById(R.id.et_search);
            btn_search = layout.findViewById(R.id.btn_search);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //Enable自定义的View
            actionBar.setCustomView(layout);//设置自定义的布局：actionbar_custom
            actionBar.setDisplayHomeAsUpEnabled(true);
            Toolbar parent = (Toolbar) layout.getParent();
            parent.setContentInsetsAbsolute(0,0);
            //设置actionbar 背景色
            int color = Color.parseColor("#ffffff");
            ColorDrawable drawable = new ColorDrawable(color);
            actionBar.setBackgroundDrawable(drawable);
        }

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }
    private void initData() {
        gson = new Gson();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        editor = preferences.edit();
        String notesStr = preferences.getString("notes", "");

        Log.i("search_activity", "initData: "+notesStr);

        if (notesStr!=""){
            strings = gson.fromJson(notesStr,new TypeToken<ArrayList<String>>() {}.getType());
        } else {strings = new ArrayList<>();}
        adapter = new SearchRecyclerViewAdapter(this,strings);
        manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayout.VERTICAL);
        manager.setSmoothScrollbarEnabled(false);
        recyclerView.setLayoutManager(manager);
    }
    private void setListener() {
        //设置 edit监听器
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Search_Activity.this,ResultByName.class);
                if (et_search.getText().toString()=="" || et_search.getText().toString()==null){
                    Toast.makeText(Search_Activity.this,"输入内容不能为空!!!",Toast.LENGTH_SHORT);
                } else {
                    strings.add(et_search.getText().toString());
                    Log.i("SearchActivity.btn_search.setOnClickListener", "onClick: "+strings.toString());
                    editor.putString("notes",gson.toJson(strings));
                    editor.apply();
                    intent.putExtra("search_text",et_search.getText().toString());
                    startActivity(intent);
                }

            }
        });
    }

    private void updataView() {
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String notesStr = preferences.getString("notes", "");
        editor = preferences.edit();
        Log.i("search_activity", "initData: "+notesStr);

        if (notesStr!=""){
            strings = gson.fromJson(notesStr,new TypeToken<ArrayList<String>>() {}.getType());
        } else {strings = new ArrayList<>();}
        adapter.update(strings);
    }
    public void clear(View view){
        editor.putString("notes",gson.toJson(new ArrayList<String>()));
        editor.apply();
        adapter.update(new ArrayList<String>());
    }
}
